using Bzstd.Entity;
using Bzstd.Service;
using Microsoft.AspNetCore.Mvc;

namespace Bzstd.Controllers;

[ApiController, Route("api/{action}")]
public class P2Controller(P2Service p2Service) : ControllerBase
{
	[HttpGet, ActionName("init")]
	public void Init()
	{
		p2Service.Init();
		p2Service.Combine();
	}

	[HttpGet, ActionName("puzzleItems")]
	public List<PuzzleItem> GetWorkshopItems()
	{
		return p2Service.CombinePuzzleItems;
	}

	[HttpGet, ActionName("bind")]
	public void Bind(string puzzleFileName, uint fileId)
	{
		p2Service.Bind(puzzleFileName, fileId);
	}

	[HttpGet, ActionName("previewFile")]
	public object OpenPreviewFile()
	{
		return p2Service.OpenPreviewFile();
	}

	[HttpGet, ActionName("mapFile")]
	public object OpenMapFile()
	{
		return p2Service.OpenMapFile();
	}

	[HttpGet, ActionName("rename")]
	public void Rename(string oldName, string newName)
	{
		p2Service.Rename(oldName, newName);
	}

	[HttpPost, ActionName("submit")]
	public void Submit(PuzzleItem puzzleItem)
	{
		p2Service.Submit(puzzleItem);
	}

	[HttpGet, ActionName("execCommand")]
	public void ExecCommand(string command)
	{
		p2Service.ExecCommand(command);
	}

	[HttpGet, ActionName("showInWorkshop")]
	public void ShowInWorkshop(ulong fileId)
	{
		p2Service.ShowInWorkshop(fileId);
	}

	[HttpGet, ActionName("queryWorkshopItem")]
	public PuzzleItem QueryWorkshopItem(ulong fileId)
	{
		return p2Service.QueryWorkshopItem(fileId);
	}

	[HttpPost, ActionName("cloneMap")]
	public void CloneMap(PuzzleItem puzzleItem)
	{
		p2Service.CloneMap(puzzleItem);
	}

	[HttpGet, ActionName("query")]
	public List<PuzzleItem> Query([FromQuery] QueryInfo queryInfo)
	{
		return p2Service.Query(queryInfo).Where(item => !item.Workshop.MapFileName.EndsWith(".exe")).ToList();
	}

	[HttpGet, ActionName("operate")]
	public void Operate(ulong fileId, string type)
	{
		p2Service.Operate(fileId, type);
	}
}
